package bean;

import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import dao.ClientDAO;
import dao.JPA;
import dao.TreatmentActDAO;
import entity.TreatmentAct;

@ManagedBean
@ViewScoped
public class CreateTreatmentActBean {

	@Inject @JPA
	private TreatmentActDAO treatmentActDAO;
	@Inject @JPA
	private ClientDAO clientDAO;
	private TreatmentAct treatmentAct = new TreatmentAct();	
	private int clientId;
	
	@PostConstruct
	private void init() {
		Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		if(params.get("clientId") != null) { 
			clientId = Integer.valueOf(params.get("clientId"));
			treatmentAct.setClient(clientDAO.getEntityById(clientId));
		}
	}
	
	public TreatmentAct getTreatmentAct() {
		return treatmentAct;
	}
	
	public String createTreatmentAct() {
		treatmentActDAO.persist(treatmentAct);	
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Treatment act created", "Treatment act created"));
		return "viewTreatmentHistory.xhtml?clientId=" + clientId + "&faces-redirect=true";
	}
		
	public String goBack() {
		return "viewTreatmentHistory.xhtml?clientId=" + clientId + "&faces-redirect=true";
	}
	
}